package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.model.TbItemCat;
import com.dongdongshop.model.TbItemCatExample;
import com.dongdongshop.model.TbTypeTemplateExample;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.vo.ItemCatVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Resource
    private TbItemCatMapper tbItemCatMapper;

    @Override
    public List<ItemCatVO> getAllItem(Long parentId) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(example);
        List<ItemCatVO> voList = tbItemCats.stream().map(tbItemCat -> {
            ItemCatVO vo = new ItemCatVO();
            BeanUtils.copyProperties(tbItemCat, vo);
            return vo;
        }).collect(Collectors.toList());
        return voList;
    }

    @Override
    public void addItemCat(ItemCatVO itemCatVO) {
        TbItemCat tbItemCat = new TbItemCat();
        BeanUtils.copyProperties(itemCatVO,tbItemCat);
        tbItemCatMapper.insertSelective(tbItemCat);
    }

    @Override
    public ItemCatVO getItemById(Long id) {
        if(id == 0){
            return null;
        }
        TbItemCat tbItemCat = tbItemCatMapper.selectByPrimaryKey(id);
        ItemCatVO vo = new ItemCatVO();
        BeanUtils.copyProperties(tbItemCat, vo);
        return vo;
    }

    @Override
    public void updateItemCat(ItemCatVO itemCatVO) {
        TbItemCat tbItemCat = new TbItemCat();
        BeanUtils.copyProperties(itemCatVO,tbItemCat);
        tbItemCatMapper.updateByPrimaryKeySelective(tbItemCat);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        tbItemCatMapper.deleteByExample(example);
    }

    @Override
    public List<ItemCatVO> getAll() {
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(null);
        List<ItemCatVO> voList = tbItemCats.stream().map(tbItemCat -> {
            ItemCatVO vo = new ItemCatVO();
            BeanUtils.copyProperties(tbItemCat, vo);
            return vo;
        }).collect(Collectors.toList());
        return voList;
    }
}
